﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.WorkFlow;
using Enterprise.Data.WorkFlow;
using Enterprise.Infrastructure;
using System.Data;
using Enterprise.Service.User;
namespace Enterprise.Service.WorkFlow
{
    /// <summary>
    /// 流程处理过程记录服务
    /// </summary>
   public class WorkFlowProcessService
    {

        /// <summary>
        /// 流程处理过程缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-WorkFlowProcess";

        /// <summary>
        /// 流程处理过程列表 返回所有流程记录
        /// </summary>
        /// <returns></returns>
        public List<WorkFlowProcessEntity> WorkFlowProcessList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<WorkFlowProcessEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                WorkFlowProcessData data = new WorkFlowProcessData();
                List<WorkFlowProcessEntity> list = data.WorkFlowProcessList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

       /// <summary>
       /// 返回流程审核记录
       /// </summary>
       /// <param name="wfId"></param>
       /// <returns></returns>
        public List<WorkFlowProcessEntity> WorkFlowProcessListByWorkFlow(string wfId)
        {
            return WorkFlowProcessList().Where(p => p.WFID == wfId && p.PISCHECK == 1).OrderBy(p => p.PUSERORDERBY).ToList();
        }

       /// <summary>
        /// 转到下一个流程审核人
       /// </summary>
       /// <param name="workFlowId"></param>
       /// <param name="checkUserId"></param>
       /// <param name="userId"></param>
       /// <param name="workFlowType"></param>
       /// <param name="iIndex"></param>
        public void WorkFlowProcessInsertNew(string workFlowId, int checkUserId,int userId, int workFlowType, int iIndex)
        {
            WorkFlowProcessEntity entity = new WorkFlowProcessEntity();
            entity.DB_Option_Action = "Insert";
            entity.PID = Guid.NewGuid().ToString();
            entity.WFID = workFlowId;
            entity.PWFTYPE = workFlowType;
            entity.PUSERID_REAL = checkUserId;
            entity.PUSERID = userId;
            entity.PISCHECK = 0;
            entity.PRESULT = 0;
            entity.PREMARK = "";
            entity.PTIME = DateTime.Now;
            entity.PUSERORDERBY = iIndex;
            var q = WorkFlowProcessList().Where(p => p.WFID == workFlowId && p.PWFTYPE == entity.PWFTYPE &&p.PUSERID_REAL==entity.PUSERID_REAL&& p.PUSERID == entity.PUSERID && p.PUSERORDERBY == entity.PUSERORDERBY).FirstOrDefault();
            if (q == null)
            {
                WorkFlowProcessExecute(entity);
            }      
        }

        /// <summary>
        /// 清楚缓存
        /// </summary>
        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

       /// <summary>
       ///  删除审批流程
       /// </summary>
       /// <param name="workFlowId"></param>
        public void WorkFlowProcessDelete(string workFlowId)
        {
            string cmd = "Delete from SYS_WORKFLOWPROCESS where WFID='" + workFlowId + "'";
            if (!string.IsNullOrEmpty(cmd))
            {
                WorkFlowProcessExecuteBySql(cmd, 1);
            }
        }

        /// <summary>
        /// 添加、修改
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void WorkFlowProcessExecute(WorkFlowProcessEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_WORKFLOWPROCESS(PID,WFID,PWFTYPE,PUSERID_REAL,PUSERID,PISCHECK,PUSERORDERBY) values('" + entity.PID + "','" + entity.WFID + "'," + entity.PWFTYPE + "," + entity.PUSERID_REAL + "," + entity.PUSERID + "," + entity.PISCHECK + "," + entity.PUSERORDERBY + ")";
                    break;
                case "Update":
                    cmd = "Update SYS_WORKFLOWPROCESS set PISCHECK=" + entity.PISCHECK + " , PRESULT=" + entity.PRESULT + " , PREMARK='" + entity.PREMARK + "',PTIME=" + Utility.FormatOracleDate(entity.PTIME) + " where PID='" + entity.PID + "'";
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                WorkFlowProcessExecuteBySql(cmd, 1);
            }
        }

        /// <summary>
        /// 操作方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1:刷新缓存 0:不刷新缓存 </param>
        public void WorkFlowProcessExecuteBySql(string cmd, int typeId)
        {
            WorkFlowNodeData data = new WorkFlowNodeData();
            data.WorkFlowExecute(cmd);
            if (typeId == 1)
            {
                ClearCache();
            }
        }

        #region "静态方法"
        /// <summary>
        /// 流程审批单当前审批状态
        /// </summary>
        /// <param name="workFlowId">流程单编码</param>
        /// <param name="workFlowState">流程单状态</param>
        /// <returns></returns>
        public static string WorkFlowProcessCurrentState(string workFlowId, int workFlowState)
        {
            if (workFlowState == 1)
            {
                return "审核通过";
            }
            else if (workFlowState == 2)
            {
                return "审核未通过";
            }
            else
            {
                WorkFlowProcessService wpService = new WorkFlowProcessService();
                var q = wpService.WorkFlowProcessList().Where(p => p.WFID == workFlowId && p.PISCHECK == 0).OrderBy(p => p.PUSERORDERBY).FirstOrDefault();
                if (q != null)
                {
                    return string.Format("待[{0}]审核", UserService.GetUserName(q.PUSERID));
                }
            }
            return "&nbsp;";
        }
        #endregion

    }
}
